local exinwan_Effect = fk.CreateSkill {

  name = "exinwan_effect",

  tags = { Skill.Compulsory },

}



exinwan_Effect:addEffect(fk.AfterCardsMove, {
  name = "exinwan_effect",
  global = true,
  mute=true,
  priority = 0, -- game rule
  can_trigger = function(_, event, target, player, data)
    local room = player.room
    local move_event = room.logic:getCurrentEvent()
    local parent_event = move_event.parent
    local card_ids = {}
    if parent_event ~= nil then
      if parent_event.event == GameEvent.Pindian then
        local pindianData = parent_event.data
        if pindianData.from == player then
          if pindianData.fromCard == nil then
            return false
          end
          card_ids = room:getSubcardsByRule(pindianData.fromCard)
        else
          for toId, result in pairs(pindianData.results) do
            if player == toId then
              if result.toCard == nil then
                return false
              end
              card_ids = room:getSubcardsByRule(result.toCard)
              break
            end
          end
        end
      end
    end
    for _, move in ipairs(data) do
      if (move.toArea == Card.Processing or move.toArea == Card.DiscardPile) then
        if move.from == player then
          for _, info in ipairs(move.moveInfo) do
            if (info.fromArea == Card.PlayerHand or info.fromArea == Card.PlayerEquip) and
                Fk:getCardById(info.cardId).name == "exinwan" then
              return true
            end
          end
        elseif #card_ids > 0 then
          for _, info in ipairs(move.moveInfo) do
            if info.fromArea == Card.Processing and table.contains(card_ids, info.cardId) and
                Fk:getCardById(info.cardId).name == "exinwan" then
              return true
            end
          end
        end
      end
    end
  end,
  on_use = function(_, event, _, player, data)
    local room = player.room
    local to
    room:broadcastPlaySound(Thbattle_path .. "audio/card/male/exinwan")
    for _, move in ipairs(data) do
      if move.proposer == nil then
        local targetplayer = room.logic:getCurrentEvent():findParent(GameEvent.UseCard, true)
        if targetplayer == nil then
          to = move.from
        else
          to = targetplayer.data.from
        end
      else
        to = move.proposer
      end
    end
    if to == nil then return false end
    if #to:getCardIds("he") > 1 and not to.dead then
      local choose = room:askForChoice(to, { "#exinwan-choose1", "#exinwan-choose2" }, "exinwan_effect")
      if choose == "#exinwan-choose1" then
        local targetcard = room:askForCardsChosen(to, to, 2, 2, "he", "exinwan_effect", "#exinwan_skill-choose")
        if #targetcard == 2 then
          room:throwCard(targetcard, "exinwan_effect", to)
        else
          room:damage({
            to = to,
            damage = 1,
          })
        end
      else
        room:damage({
          to = to,
          damage = 1,
        })
      end
    else
      room:damage({
        to = to,
        damage = 1,
      })
    end
  end,
})

return exinwan_Effect
